/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package  org.cestum.cave.presenter.wave {
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import com.hurlant.util.der.ByteString;

/**
 * Enforces consistency between serialised representations of protocol buffers (stored as
 * {@code ByteString}s) and their types.
 *
 *
 */
public final class ByteStringMessage {
  private  var byteString:ByteString;
  
  private  var message:Message;

  /**
   * Create a {@code ByteStringMessage} from a prototype of the message and its {@code ByteString}
   * representation.
   *
   * @param prototype to create the {@code Message} from
   * @param byteString representation of the message
   * @throws InvalidProtocolBufferException if byteString is not a valid protocol buffer
   */
  public static function from(prototype:Message, byteString:ByteString):ByteStringMessage {
    return new ByteStringMessage(prototype, byteString);
  }

  /**
   * Create a {@code ByteStringMessage} from the message itself.  This should only ever be used when
   * committing the serialised version of a message, otherwise it defeats the purpose of this class.
   *
   * @param message to form the serialised version of
   */
  
//  public static function fromMessage(message:Message):ByteStringMessage {
//    try {
//      return (ByteStringMessage) from(
//          message.getDefaultInstanceForType(), message.toByteString());
//    } catch (e) {
//      // This cannot possibly fail, unless the protocol buffer library is broken
//      throw new Error(e);
//    }
//  }

  
  function ByteStringMessage(prototype:Message, byteString:ByteString) {
    this.message = Message(prototype.newBuilderForType().mergeFromByteArray(byteString).build());
    this.byteString = byteString;
  }

  /**
   * @return the immutable underlying {@code Message}
   */
  public function getMessage():Message {
    return message;
  }

  /**
   * @return the serialised representation of this message
   */
  public function getByteString():ByteString {
    return this.byteString;
  }

//  /**
//   * @return the serialised byte array representation of this message
//   */
//  public byte[] getByteArray() {
//    return this.byteString.toByteArray();
//  }
//
//  
  
  public function equals(o:Object):Boolean {
    if (!(o instanceof ByteStringMessage)) {
      return false;
    } else {
      var bsm:ByteStringMessage= ByteStringMessage(o);
      return byteString == (bsm.byteString);
    }
  }

//  
//  public function hashCode():int {
//    return byteString.hashCode();
//  }

  
  public function toString():String {
    return "ByteStringMessage: " + message;
  }
}
}